package com.electromagnetic.industry.software.manage.service.serviceimpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrFormatter;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.electromagnetic.industry.software.common.enums.EffectFlagEnum;
import com.electromagnetic.industry.software.common.util.EleIdCreator;
import com.electromagnetic.industry.software.common.util.UserThreadLocal;
import com.electromagnetic.industry.software.manage.mapper.FileFormatMapper;
import com.electromagnetic.industry.software.manage.pojo.models.FileFormat;
import com.electromagnetic.industry.software.manage.pojo.resp.FileFormatVO;
import com.electromagnetic.industry.software.manage.service.FileFormatService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FileFormatServiceImpl extends ServiceImpl<FileFormatMapper, FileFormat> implements FileFormatService {

    /**
     * 创建文件格式
     *
     * @param suffixName
     * @return
     */
    @Override
    public boolean addFileFormat(String suffixName) {

        Assert.isTrue(suffixName.matches("^[a-zA-Z0-9]+$"), "文件格式不符合规范");
        List<FileFormat> fileFormats = this.baseMapper.selectList(null);
        List<String> names = fileFormats.stream().map(FileFormat::getSuffixName).toList();

        // 有可能是被废除了，现在恢复
        if (names.contains(suffixName)) {
            this.baseMapper.update(new FileFormat(), Wrappers.<FileFormat>lambdaUpdate().set(FileFormat::getEffectFlag, EffectFlagEnum.EFFECT.code).eq(FileFormat::getSuffixName, suffixName));
        } else {
            FileFormat fileFormat = new FileFormat();
            fileFormat.setSuffixName(suffixName);
            fileFormat.setEffectFlag(EffectFlagEnum.EFFECT.code);
            fileFormat.setId(EleIdCreator.createSnowflakeId());
            fileFormat.setSuffixNo(createFileFormatNo(fileFormats.size()));
            fileFormat.setCreatedBy(UserThreadLocal.getUserId());
            this.baseMapper.insert(fileFormat);
        }
        UserThreadLocal.setSuccessInfo("", "", StrFormatter.format("添加了文件格式 {} ", suffixName));
        return true;
    }

    private String createFileFormatNo(int size) {
        if (size >= 0 && size <= 8) {
            return "0" + (size + 1);
        }
        return (size + 1) + "";

    }

    /**
     * 删除文件格式
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteFileFormat(String id) {
        this.baseMapper.update(new FileFormat(), Wrappers.<FileFormat>lambdaUpdate().eq(FileFormat::getId, id).set(FileFormat::getEffectFlag, EffectFlagEnum.NOT_EFFECTIVE.code));
        UserThreadLocal.setSuccessInfo("", "", StrFormatter.format("废除了文件格式，id {} ", id));
        return true;
    }

    /**
     * 查询了文件格式列表
     *
     * @return
     */
    @Override
    public List<FileFormatVO> getList() {
        List<FileFormat> fileFormats = this.baseMapper.selectList(Wrappers.<FileFormat>lambdaQuery().eq(FileFormat::getEffectFlag, EffectFlagEnum.EFFECT.code).orderByAsc(FileFormat::getSuffixNo));
        List<FileFormatVO> fileFormatVOS = BeanUtil.copyToList(fileFormats, FileFormatVO.class);
        UserThreadLocal.setSuccessInfo("", "", "查询了文件格式列表");
        return fileFormatVOS;
    }

    /**
     * 更新文件格式
     *
     * @param fileFormat
     * @return
     */
    @Override
    public boolean updateFileFormat(FileFormat fileFormat) {
        return true;
    }

}

